/**
 * @title MyWebChromeClient.java
 * @package com.esun.cheetah.view.personal.recharg
 * @description webView客户端帮助类
 * @author liugan
 * @date 2012-4-25
 * @copyright 深圳市易讯天空
 */
package com.esun.cheetah.view.personal.recharg;

import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.graphics.Bitmap;
import android.text.Html;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;

import com.esun.cheetah.view.custom.CustomAlertDialog;
import com.esun.cheetah.view.custom.CustomConfirmDialog;
import com.esun.cheetah.view.custom.CustomProgressDialog;

/**
 * @title MyWebChromeClient
 * @description webview客户端辅助类、主要覆盖js的对话框样式
 * @author liugan
 */
public class MyWebChromeClient extends WebChromeClient {

	private boolean mFirstProgress = true;
	private CustomProgressDialog mProgressDialog;

	/**
	 * 覆盖默认的window.alert展示界面，避免title里显示为“：来自file:////”
	 */
	public boolean onJsAlert(WebView view, String url, String message,
			final JsResult result) {
		final CustomAlertDialog dialog = new CustomAlertDialog(
				view.getContext());
		dialog.setOnKeyListener(new OnKeyListener() {

			@Override
			public boolean onKey(DialogInterface dialog, int keyCode,
					KeyEvent event) {
				return true;
			}
		});
		// 禁止响应按back键的事件
		dialog.setCancelable(false);

		// 响应返回
		dialog.setOnButtonClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				result.confirm();
				dialog.dismiss();
			}
		});
		dialog.setMessage(Html.fromHtml(message));
		dialog.show();

		return true;
	}

	public boolean onJsBeforeUnload(WebView view, String url, String message,
			JsResult result) {
		return super.onJsBeforeUnload(view, url, message, result);
	}

	/**
	 * 覆盖默认的window.confirm展示界面，避免title里显示为“：来自file:////”
	 */
	public boolean onJsConfirm(WebView view, String url, String message,
			final JsResult result) {
		final CustomConfirmDialog dialog = new CustomConfirmDialog(
				view.getContext());

		// 屏蔽keycode等于84之类的按键，避免按键后导致对话框消息而页面无法再弹出对话框的问题
		dialog.setOnKeyListener(new OnKeyListener() {
			@Override
			public boolean onKey(DialogInterface dialog, int keyCode,
					KeyEvent event) {
				return true;
			}
		});

		// 禁止响应按back键的事件
		dialog.setCancelable(false);
		dialog.setOnCancelListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				result.cancel();
				dialog.dismiss();
			}
		});

		dialog.setOnConfirmListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				result.confirm();
				dialog.dismiss();

			}
		});

		dialog.setMessage(Html.fromHtml(message));
		dialog.show();
		return true;
	}

	/**
	 * 覆盖默认的window.prompt展示界面，避免title里显示为“：来自file:////”
	 * window.prompt('请输入您的域名地址', '618119.com');
	 */
	public boolean onJsPrompt(WebView view, String url, String message,
			String defaultValue, final JsPromptResult result) {
		return super.onJsPrompt(view, url, message, defaultValue, result);
	}

	/**
	 * 装载的时候显示Loading...
	 */
	@Override
	public void onProgressChanged(WebView view, int newProgress) {
		if (mFirstProgress && newProgress != 100) {
			mFirstProgress = false;
			if (mProgressDialog == null) {
				mProgressDialog = new CustomProgressDialog(view.getContext());
				mProgressDialog.setMessage("Loading...");
			}
			mProgressDialog.show();
		}

		if (newProgress == 100 && mProgressDialog != null) {
			mProgressDialog.dismiss();
			mFirstProgress = true;
		}
	}

	@Override
	public void onReceivedIcon(WebView view, Bitmap icon) {
		super.onReceivedIcon(view, icon);
	}

	@Override
	public void onReceivedTitle(WebView view, String title) {
		super.onReceivedTitle(view, title);
	}

	@Override
	public void onRequestFocus(WebView view) {
		super.onRequestFocus(view);
	}

}
